<?php
/**
 * 数据统计
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/12/12
 * Time: 10:14
 */
namespace app\admin\controller;
use think\Db;
class Count extends Base {
 /**
  * 订单统计
  */
 public function OrderCount() {
  if (request()->isAjax()) {
   $star_time = request()->post('star_time', 0);
   $data = [];
   if ($star_time == 0) {
    $days = date("t");
    $firstday = date("Y-m", time());
    //订单
    //1.当月数量
    $order_month = Db::name('ybsc_order')->where('mch_id', $this->bus_id)->whereTime('create_time', 'month')->count();
    //2.当月价钱
    $order_pay = Db::name('ybsc_order')->where('mch_id', $this->bus_id)->whereNotIn('order_status', 5)->whereTime('create_time', 'month')->sum('order_money');
    $data[2] = $order_month;
    $data[3] = sprintf("%.2f", $order_pay);
   } else {
    $days = date('t', strtotime($star_time));
    $firstday = $star_time;
    //订单
    //1.当月数量
    $order_month = Db::name('ybsc_order')->where('mch_id', $this->bus_id)->whereTime('create_time', 'between', [$firstday, $firstday . "-" . $days . " 23:59:59"])->count();
    //2.当月价钱
    $order_pay = Db::name('ybsc_order')->where('mch_id', $this->bus_id)->whereTime('create_time', 'between', [$firstday, $firstday . "-" . $days . " 23:59:59"])->sum('order_money');
    $data[2] = $order_month;
    $data[3] = sprintf("%.2f", $order_pay);
   }
   for ($i = 1; $i <= $days; $i++) {
    $data[0][] = $i . "日";
    $data[1][] = Db::name('ybsc_order')->where('mch_id', $this->bus_id)
        ->whereTime('create_time', 'between', [$firstday . "-" . $i, $firstday . "-" . $i . " 23:59:59"])
        ->count();
   }
   return $data;
  } else {
   $star_time = request()->param('star_time','');
   $this->assign('star_time', $star_time);
   return view("count/order_count");
  }
 }
 /**
  * 会员统计
  */
 public function UserCount() {
  if (request()->isAjax()) {
   $star_time = request()->post('star_time', 0);
   $data = [];
   if ($star_time == 0) {
    $days = date("t");
    $firstday = date("Y-m", time());
   } else {
    $days = date('t', strtotime($star_time));
    $firstday = $star_time;
   }
   for ($i = 1; $i <= $days; $i++) {
    $data[0][] = $i . "日";
    $data[1][] = Db::name('ybsc_user')->where('mch_id', $this->bus_id)
        ->whereTime('reg_time', 'between', [$firstday . "-" . $i, $firstday . "-" . $i . " 23:59:59"])
        ->count();
   }
   return $data;
  } else {
   $star_time = request()->param('star_time','');
   $this->assign('star_time', $star_time);
   return view("count/user_count");
  }
 }
 /**
  * 商品列表
  */
 public function goods_count() {
  $goodservice = new \app\admin\service\Goods();
  $search_text = input('param.search_text');
  $where = [];
  $where2=[];
   $where['ng.state'] = ['eq', '1'];
   if (!empty($search_text)) {
    $where['ng.goods_name'] = ['like', '%' . $search_text . '%'];
   }
   $star_time = input('param.star_time');
   $end_time = input('param.end_time');
   if (!empty($star_time)) {
    $star = strtotime($star_time);
    $where2['o.create_time'] = ['between', [$star, $star + 86400]];
   }
   if (!empty($star_time) && !empty($end_time)) {
    $star = strtotime($star_time);
    $end = strtotime($end_time);
    $where2['o.create_time'] = ['between', [$star, $end]];
   }
   $where['ng.is_del'] = array('<>', '1');
   $where['ng.mch_id'] = array('eq', $this->bus_id);
   $cate_id = input('param.goods_cate');
   if (!empty($cate_id) || $cate_id != 0) {
    $where['ng.cate_id'] = array('eq', $cate_id);
   }
   $result = $goodservice->getGoodsList($where);
  $where2['o.order_status']=3;
   foreach($result as $k=>$v){
    $where2['g.goods_id']=$v['goods_id'];
    $num=Db::name('ybsc_order_goods')->alias('g')
        ->join('ybsc_order o','g.order_id=o.order_id','left')
        ->where($where2)->count();
    $result[$k]['num']=$num;
   }
   $page = $result->render();
   $this->assign('page', $page);
   $this->assign('cate_id', $cate_id);
   $this->assign('result', $result);
   $this->assign('search_text', $search_text);
   $this->assign('star_time', $star_time);
   $this->assign('end_time', $end_time);
   //查询当前用户商品分类
   $goods_cate = Db::name('ybsc_goods_cate')->where('is_visible=1')->where('mch_id', $this->bus_id)->select();
   $this->assign('goods_cate', $goods_cate);
   return view('count/goods_count');
 }
}